package com.sykj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.pojo.Permission;
import com.sykj.servies.PermissionServies;
import com.sykj.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/config")
public class PermissionController {
    //权限
    @Autowired
    PermissionServies permissionServies;


    /**
     * 权限列表
     * @param model
     * @param page
     * @return
     */
    //查询显示到表
    @RequestMapping("/listPermission")
    public String list(Model model, Page page){
        //调用分页插件**第几页开始显示。每页显示几条数据
        PageHelper.offsetPage(page.getStart(),page.getCount());
        List<Permission> ps= permissionServies.list();
        //查询的数据传入，查出总条数
        int total = (int) new PageInfo<>(ps).getTotal();
        page.setTotal(total);

        model.addAttribute("ps", ps);
        model.addAttribute("perCount",ps.size());
        return "syspage/admin-permission";
    }
    /**
     * 添加权限页面
     * @return
     */
    //点击添加权限，跳出子窗口
    @RequestMapping("/adminPerAddUI")
    public String addUI(){

        return "syspage/admin-permission-add";
    }
    //添加
    @RequestMapping("/addPermission")
    public String list(Model model,Permission permission){
        permissionServies.add(permission);
        return "redirect:listPermission";
    }

    //点击修改
    @RequestMapping("/editPermission")
    public String list(Model model,long id){
        //
        Permission permission =permissionServies.get(id);
        model.addAttribute("permission", permission);
        return "syspage/admin-permission-edit";
    }

    //修改权限
    @RequestMapping("/updatePermission")
    public String update(Permission permission){
        permissionServies.update(permission);
        //重定向刷新
        return "redirect:listPermission";
    }

    //根据角色id删除
    @RequestMapping("/deletePermission")
    public String delete(Model model,long id){
        permissionServies.delete(id);
        //重定向刷新
        return "redirect:listPermission";
    }
}

